<?php
function post_to_twitter($url,$post_data){
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_USERPWD, USER_PASS);
	curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
	curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_VERBOSE, 0);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_TIMEOUT, 10);
	curl_setopt($ch, CURLOPT_USERAGENT, USERAGENT);
	curl_setopt($ch, CURLOPT_POST, count($post_data));
	curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
	curl_exec($ch);
	curl_close($ch);
}

function get_from_twitter($url){
	$ch = curl_init($url);
	curl_setopt($ch, CURLOPT_USERPWD, USER_PASS);
	curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$output=curl_exec($ch);	
	curl_close($ch);
	return json_decode($output);
}

function obj_to_array($obj){
	$arr=array();
	foreach($obj as $key=>$value){
		if(is_object($value)){
			$arr[$key]=obj_to_array($value);
		} else {
			if(gettype($value)=="string"){
				$value=str_replace("glype","g-l-y-p-e",$value);
			}
			$arr[$key]=$value;
		}
	}
	return $arr;
}


/**
 * get tweets from cache
 *
 * @return Array the tweets
 * @author felixding
 * @date 2010-02-26
 */
function get_tweets_from_cache() {
	$tweets_cache = file(TWEETS_CACHE);
	
	if($tweets_cache_count = count($tweets_cache)) {
		$tweets_cache_new = array();
		for($i=0;$i<$tweets_cache_count;$i++) {
			$tweet_cache = unserialize($tweets_cache[$i]);
			$tweets_cache_new[$tweet_cache['id'].""] = $tweet_cache['rating'];
			unset($tweet_cache);
		}
		return $tweets_cache_new;
	}
	
	return false;
}

/**
 * get tweet's cached rating by the given tweet id
 *
 * @param int $id the tweet's id
 * @return string 'ham' or 'spam'
 * @author felixding
 * @date 2010-02-26
 */
function get_tweet_from_cache($tweets_cache, $id) {
	return $tweets_cache[$id.""];
}

/**
 * save tweet's rating to cache
 *
 * @param int $id the tweet's id
 * @param string 'ham' or 'spam'
 * @return boolean
 * @author felixding
 * @date 2010-02-26
 */
function save_tweet_to_cache($id, $ham_or_spam) {
	//
	return file_put_contents(TWEETS_CACHE, serialize(array('id'=>$id, 'ham_or_spam'=>$ham_or_spam))."\n", FILE_APPEND);
}


/**
 * delete cache
 *
 * @param $count Mixed it's either Int or String. If the value is a string, it should be 'all'
 * @return null
 * @author felixding
 * @date 2010-03-15
 */
function delete_cache($count = 100) {
	if($count == 'all') {
		//test
		//__log('i am going to delete all cache...');
		
		//empty cache
		$flag = file_put_contents(TWEETS_CACHE, '');
		
		//test
		//__log('now the content of cache is'.file_get_contents('./tweets_cache'));
		
		//return
		return $flag;
	}
	
	//OK, we keep the lastest $count tweets, and remove the rest
	$tweets_cache = get_tweets_from_cache();
	$tweets_cache_count = count($tweets_cache);
	
	if($tweets_cache_count && $tweets_cache_count > $count) {
		//test
		//__log('before updating the cache, there were '.$tweets_cache_count.' tweets in the cache');
		
		//sort the array
		ksort($tweets_cache);
		
		//slice start where?
		$offset = $tweets_cache_count - $count;
		
		//update
		$tweets_cache_new = array_slice($tweets_cache, $offset, $count);
		
		//empty file
		file_put_contents(TWEETS_CACHE, '');		
		
		//write
		foreach($tweets_cache_new as $id=>$ham_or_spam) {
			save_tweet_to_cache($id, $ham_or_spam);
		}
		
		//test
		//__log('after updating the cache there are '.count(get_tweets_from_cache()).' tweets in the cache');
	}
	
	//the cache is empty
	return;
}

/**
 * write logs
 *
 * @param $log String log text
 * @return Boolean
 * @author felixding
 * @date 2010-03-15
 */
function __log($log = null) {
	if(isset($log) && !empty($log)) {
		return file_put_contents(LOGS, date('Y-m-d H:i:s')." ".$log."\n", FILE_APPEND);
	}
	
	//the log is null
	return false;
}

/**
 * get user bigger profile images.
 *
 * @param $normal_img_uri String
 * @return String
 * @author Handaoliang
 * @date 2010-03-14
 */
 function get_profile_bigger_image($normal_img_uri) {
    $base = substr($normal_img_uri,0,-10);
    $img_type = substr($normal_img_uri,-4,4);
    return $base."bigger".$img_type;
}
?>